package p.ithorns.integration.transform.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import p.ithorns.integration.transform.entity.User;
import p.ithorns.tools.translator.annotation.Translatable;
import p.ithorns.tools.translator.annotation.Translator;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * UserNameTransfomer
 *
 * @author Ht.L
 * @version 1.0.0
 * @date 2023/4/5 19:29
 */
@Component
public class UserNameTransformer implements Translator<Integer, Translatable> {

    private UserService userService;

    @Autowired
    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    @Override
    public String translate(Translatable annotation, Integer originalValue) {
        return userService.getDict(originalValue);
    }

    public Map<Integer, Object> translate(Translatable annotation, Set<Integer> ids) {
        List<User> users = userService.listByIds(ids);
        //  refVal -> id
        return users.stream()
                .collect(Collectors.toMap(User::getId, User::getName));
    }

}